// vuex的状态机,根据功能模块划分为设备管理，设备点检，设备维修，设备保养等
import { fetchListNoPage, fetchList, startUserInfo, stopUserInfo, fetchBroadInfo, saveInfo, saveDeviceInfo, fetchDetailInfo, exportFile, downloadTemplete, deleteDeviceInfo, sortDepartmentInfo } from '@/api/point-check/itemgroups'
import { reduceNullContent, deepClone } from '@/utils/index'
const state = {
  listSearchParams: {},
  paginationInfo: {},
  records: [], // 表格数据
  pages: 1,
  total: 20,
  mesg: '',
  /**
     * 编辑相关
     */
  broadInfo: [], // 动态加载后端配置的扩展信息
  detailInfo: {}, // 详情模板
  totalForm: {}
}
const formModel = {}
const mutations = {

  SET_LISTSERACH_PARAMS: (state, args) => {
    state.listSearchParams = args
  },
  SET_LISTDATA: (state, args) => {
    state.records = args.records
    state.pages = args.pages
    state.current = args.current
    state.total = args.total
  },
  SET_BROAD_INFO: (state, args) => {
    state.totalForm[1].formComponent = args
    state.totalForm[1].formModel = formModel
  },
  SET_TOTAL_FORM: (state, args) => {
    state.totalForm = args
  },
  SET_DETAIL_INFO: (state, args) => {
    state.detailInfo = args
  },
  SET_PAGINATION: (state, args) => {
    state.paginationInfo = args
  }
}

const actions = {
  // 设备台账查询参数
  setListSearchParams({ commit, state }, args) {
    const data = Object.assign({}, state.listSearchParams, args)
    commit('SET_LISTSERACH_PARAMS', data)
  },
  // 查询不分页列表
  getItemsGroupList({ commit, state }, args) {
    return fetchListNoPage()
  },
  // 列表
  requestDeviceList({ commit, state }, args) {
    return fetchList(reduceNullContent(args)).then((response) => {
      console.log('设备台账', response)
      commit('SET_LISTDATA', response.data)
    })
  },
  setTotalForm({ commit, state }, args) {
    console.log(args, 'total')
    commit('SET_TOTAL_FORM', args)
  },
  // 保存设备台账
  saveDeviceInfo({ commit, state }, args) {
    return saveDeviceInfo(args)
  },
  // 保存账号
  saveInfo({ commit, state }, args) {
    return saveInfo(args)
  },
  // 部门排序
  sortDepartmentInfo({ commit, state }, args) {
    return sortDepartmentInfo(args)
  },
  // 设备台账单条数据的扩展信息模板请求
  requestDeviceBroadInfo({ commit, state }, args) {
    return fetchBroadInfo().then((response) => {
      const array = response.data.map(e => {
        formModel[e.fieldCode] = ''
        e.type = e.fieldType == 1 ? 'input' : 'select'
        e.label = e.name || ''
        // e.model = e.fieldCode
        e.rules = [{ required: true, message: e.description, trigger: 'blur' }]
        return e
      })
      console.log('设备台账扩展信息', array)
      commit('SET_BROAD_INFO', array)

      // Just to simulate the time of the request
    })
  },
  // 设置设备台账对象
  setDetailInfo({ commit, state }, args) {
    commit('SET_DETAIL_INFO', args)
  },
  // 启用
  useUserInfo({ commit, state }, args) {
    return startUserInfo(args)
  },
  // 停用
  uselessUserInfo({ commit, state }, args) {
    return stopUserInfo(args)
  },
  // 请求台账详情
  requestDeviceDetailInfo({ commit, state }, args) {
    return fetchDetailInfo(args).then((response) => {
      console.log('设备台账详细信息', response)
      const data = {}
      data.detailInfo = state.detailInfo.detailInfo.map(item => {
        item.value = response['data'][item.name]
        return item
      })
      data.broadInfo = state.detailInfo.broadInfo.map(item => {
        item.value = JSON.parse(response['data'].extra)[item.name]
        return item
      })
      // data.histroyShowInfo=state.detailInfo.histroyShowInfo.map(item=>{
      //     item.value=state.detailInfo.detailInfo.archives
      //     return item
      // })
      commit('SET_DETAIL_INFO', data)

      // Just to simulate the time of the request
    })
  },
  // 导出
  exportFile({ commit, state }, args) {
    return exportFile()
  },
  // 下载批量导入模板
  downloadTemplete({ commit, state }, args) {
    const data = {}
    data.objectName = 'deviceTemplate.xls'
    data.expires = 3000
    return downloadTemplete(data)
  },
  // 删除设备台账
  deleteDeviceInfo({ commit, state }, args) {
    return deleteDeviceInfo(args)
  },
  // 设置分页器
  setPagination({ commit, state }, args) {
    const data = {}
    data.sizeChange = args.sizeChange
    data.currentChange = args.currentChange
    data.current = state.current
    data.total = state.total
    commit('SET_PAGINATION', data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
